const mongoose = require('../util/db')

const shopSchema = new mongoose.Schema({
   name:String,
   price:Number,
    num:Number
})
const Shop = mongoose.model('shops',shopSchema)

const findAll=()=>{
   //用promise来处理异步，控制层通过resolve拿数据
   return new Promise((resolve,reject)=>{
      Shop.find((err,docs)=>{
         if(err) reject()
         else resolve(docs)
      })
   }).catch()
}

const saveOne=(shopinfo)=>{
   const shop = new Shop(shopinfo)
   return new Promise((resolve, reject) => {
    shop.save((err, doc) => {
      if (err) reject()
      else resolve(doc)
    })
  }).catch()
}
const deleteOneById=(id)=>{
   return new Promise((resolve,reject)=>{
      Shop.findByIdAndRemove(id,(err)=>{
         if(err) reject()
         else resolve()
      })
   })
   
}
module.exports={
   findAll,
   saveOne,
   deleteOneById
}